﻿using System;
using System.Collections.Generic;
using System.Data;
using SdmxMl.Common;
using SdmxMl.Manager;
using SdmxMl.Structure;
using SdmxMl.GenericMetadata;
using System.IO;
using SdmxMl.SmsImportCfg;

namespace PluginMeb
{
    public class ImportNogaCorr
    {
        public const string Agency = "CH1_NOGA";
        public const string KEY_SCHULART = "SCHKT";
        public const string KEY_BILDUNG_KT = "BJKT";
        public const string KEY_KLASSE_CH = "KLCH";
        public const string KEY_BILDUNG_CH = "BJCH";
        public const string KEY_ALTER_MIN = "AMIN";
        public const string KEY_ALTER_MAX = "AMAX";
        public const string KEY_GULT_AB = "VF";
        public const string KEY_GULT_BIS = "VT";

        private MetadataSetType metadataSet;
        private MetadataStructureDefinitionType msd;
        private SmManager sm;
        const string CORR_TYPE = "LV1_08_02";


        public static MetadataSetType CreateMetaset()
        {
            MetadataSetType ms = new MetadataSetType();
            ms.AgencyId = Agency;
            ms.Id = "MS_NOGA_COR_" + CORR_TYPE;
            ms.MetadataStructureRef.AgencyId = Agency;
            ms.MetadataStructureRef.Id = "MSD_NOGA_COR_" + CORR_TYPE;
            ms.AttributeValueSetList.Clear();
            return ms;

        }
        private ExcelDataReader.ExcelDataReader LoadSources(string propertyPath)
        {
            ExcelDataReader.ExcelDataReader spreadsheet = null;
            if (propertyPath == null)
                throw new ApplicationException("Please provides path.");

            FileStream fs = null;
            try
            {
                fs = new FileStream(propertyPath, FileMode.Open, FileAccess.Read, FileShare.Read);
                spreadsheet = new ExcelDataReader.ExcelDataReader(fs);
            }
            finally
            {
                if (fs != null)
                    fs.Close();
            }

            return spreadsheet;
        }


        public void ImportFromExcel()
        {

            sm = new SmManager();

            Dictionary<int, MetadataSetType> dicoMatrix = new Dictionary<int, MetadataSetType>(28);

            // Fetch MSD and report for SAKE quest
            metadataSet = CreateMetaset();
            metadataSet.ReportRef = "R_CORR";
            msd = sm.GetArtefact(SdmxArtefactType.MetadataStructureDefinitions,
               metadataSet.MetadataStructureRef, true) as MetadataStructureDefinitionType;
            ReportStructureType report = msd.ReportStructureList.LocateItem(metadataSet.ReportRef);
            metadataSet.Name.UpdateDefaultText(msd.Name.GetForLanguage("en"));


            using (System.IO.StreamReader sr = new System.IO.StreamReader(@"C:\Doc\NOGA\CORR_" + CORR_TYPE + ".csv"))
            {
                string line = string.Empty;
                AttributeValueSetType atv = null;

                do
                {
                    line = sr.ReadLine();
                    if (line != null)
                    {
                        string[] arr = line.Split(';');
                        string src = arr[0];
                        if (src.Length == 0)
                            break;
                        src = src.Replace(".", "");
                        string dest = arr[1].Replace(".", "");

                        bool hasManu = arr.Length > 3;
                        string manu = hasManu ? arr[3] : null;
                        if (manu != null && manu.Length == 0)
                            manu = "0";



                        if (metadataSet.ReportRef == "R_CORR")
                        {
                            atv = new AttributeValueSetType(msd, report, metadataSet);
                            // The target values
                            atv.TargetValuesTypeList.SetComponentValue("SRC", src);
                            atv.TargetValuesTypeList.SetComponentValue("DEST", dest);

                            // The reported attributes (no comment at creation)
                            SetRepAttValue(atv, "PROB", new MultiLang(arr[2]));

                            if (manu != null)
                                SetRepAttValue(atv, "MANU", new MultiLang(manu));
                            //          SetRepAttValue(atv, "COMMENT", new MultiLang(src));

                            atv.ReportedAttributeList.Remove(atv.ReportedAttributeList.GetReportedAttributeOfConcept("COMMENT"));
                            metadataSet.AttributeValueSetList.Add(atv);
                        }
                        else
                        {
                            if (atv == null || atv.TargetValuesTypeList["NO"] != src)
                            {
                                atv = new AttributeValueSetType(msd, report, metadataSet);
                                metadataSet.AttributeValueSetList.Add(atv);

                                // The target values
                                atv.TargetValuesTypeList.SetComponentValue("NO", src);
                                atv.ReportedAttributeList.Clear();
                                ReportedAttributeType rptComment = new ReportedAttributeType();
                                rptComment.ConceptID = "COMMENT";
                                atv.ReportedAttributeList.Add(rptComment);

                            }
                            ReportedAttributeType rptDest = new ReportedAttributeType();
                            rptDest.ConceptID = "DEST";
                            rptDest.Value.UpdateDefaultText(arr[1]);
                            atv.ReportedAttributeList.Add(rptDest);

                            ReportedAttributeType rpt = new ReportedAttributeType();
                            rpt.ConceptID = "PROB";
                            rpt.Value.UpdateDefaultText(arr[2]);
                            rptDest.ReportedAttributeList.Add(rpt);

                            rpt = new ReportedAttributeType();
                            rpt.ConceptID = "MANU";
                            rpt.Value.UpdateDefaultText(manu);
                            rptDest.ReportedAttributeList.Add(rpt);
                        }


                    }
                } while (line != null);
            }


                SmManager smms = new SmManager();

                smms.PutArtefact(SdmxArtefactType.MetadataSet, metadataSet);
                object ob = NavigationManager.LoadFromSmManager(smms);
                // NavigationManager.RequestNavigation(ob);

        }
        private void SetRepAttValue(AttributeValueSetType at, string conceptId, MultiLang ml)
        {
            ReportedAttributeType ra = at.ReportedAttributeList.GetReportedAttributeOfConcept(conceptId);
            if (ra != null)
                ra.Value = ml == null ? new MultiLang() : ml;
        }

    }
}
